package com.yonyou.pmclouds.picture.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.StorageFactory;
import com.yonyou.pmclouds.picture.entity.PictureParamVO;
import com.yonyou.pmclouds.picture.entity.PictureVO;
import com.yonyou.pmclouds.picture.entity.PictureViewVO;
import com.yonyou.pmclouds.picture.entity.PictureWebVO;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.picture.rmiitf.PictureWebService;
import com.yonyou.pmclouds.workstopreport.entity.WorkStopVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * 协同管理图片上传接口
 */
@RestController
@RequestMapping({"/picture"})
public class PictureWebController {

    @Autowired
    private StorageFactory storageFactory;

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private PictureWebService pictureWebService;

    @PostMapping("/upload")
    public PictureViewVO[] upload(@RequestBody PictureWebVO pictureWebVO, HttpServletRequest request) throws BusinessException {
        pictureWebService.upload(pictureWebVO);
        PictureViewVO[] pictureViewVOS = storageFactory.getStorageUtil().signForAppPics(pictureWebVO.getPictures(), request);
        TypeProcess(pictureViewVOS);
        return pictureViewVOS;
    }



    @GetMapping("/query")
    public PictureViewVO[] upload(@RequestParam("pkBill") String pkBill, HttpServletRequest request) throws BusinessException {
        String[] picNames = pictureWebService.query(pkBill);
        if(null == picNames || picNames.length == 0){
            return new PictureViewVO[0];
        }
        PictureViewVO[] pictureViewVOS = storageFactory.getStorageUtil().signForAppPics(picNames, request);
        TypeProcess(pictureViewVOS);
        return pictureViewVOS;
    }

    @GetMapping("/delete")
    public void delete(@RequestParam("pkBill") String pkBill,@RequestParam("picName") String picName, HttpServletRequest request) throws BusinessException {
        pictureWebService.delete(pkBill,picName);
    }

    private void TypeProcess(PictureViewVO[] pictureViewVOS) {
        for(PictureViewVO pic : pictureViewVOS){
            String suffix = pic.getName();
            if(StringUtils.isNotEmpty(suffix)){
                if(suffix.endsWith(".xls") || suffix.endsWith(".xlsx")){
                    pic.setType("excel");
                }else if(suffix.endsWith(".zip") || suffix.endsWith(".rar")){
                    pic.setType("zip");
                }else if(suffix.endsWith(".doc") || suffix.endsWith(".docx")){
                    pic.setType("word");
                }else if(suffix.endsWith(".pdf") || suffix.endsWith(".pdf")){
                    pic.setType("pdf");
                }
            }

        }
    }
}
